import RouteConfig from "../config/router.config.js";
import Auth from "./auth.js";
import Helper from "./helper.js";

// console.log(RouteConfig, '路由配置');
const preCheck =  (url) => {
	let config = RouteConfig[url];
	if ( config ) {
		if ( !config.requireLogin || Auth.hasLogin() ) {
			return config;
		} else {
			return false;
		}
	}
	return false;
};
const Page = {
	to: (url, param={}) => {
		let res = preCheck(url);
		// 如果是微信授权页面 判断是否已经登录
		if (url === 'phone') {
			if (Auth.hasLogin()) {
				return true;
			}
		}
		if( res !== false ) {
			// console.log(Helper.renderUrlParam(param));return ;
			uni.navigateTo({
				url: res.path + Helper.renderUrlParam(param) ,
				fail: (err) => {
					console.log(err);
				}
			});
		} else {
			uni.navigateTo({
				url: RouteConfig['phone'].path,
				fail: (err) => {
					console.log( err, '跳转失败' )
				}
			})
		}
	},
	switch: (url) => {
		uni.switchTab({
			url: RouteConfig[url].path
		})
	},
	back: () => {
		uni.navigateBack({
			animationDuration: 100
		})
	}
};

export default Page;